/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import java.awt.FlowLayout;
import java.awt.Label;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Administrator
 */
public class work extends javax.swing.JFrame {
	
	class MyDialog extends JDialog {
		// 文本框，用于输入字符串
	    JTextField field;
	    // “确定”按钮
	    JButton setButton;
	    MyDialog() {
	        // 调用父类的构造函数，
	        // 第三个参数用false表示允许激活其他窗体。为true表示不能够激活其他窗体
	        super();
	 
	        // 添加Label和输入文本框
	        JPanel p1 = new JPanel();
	        JLabel label = new JLabel("<html>恭喜，游戏成功<br>请输入用户名</html>");
	        p1.add(label);
	        field = new JTextField(30);
	        p1.add(field);
	        getContentPane().add("Center", p1);
	 
	        // 添加确定和取消按钮
	        JPanel p2 = new JPanel();
	        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
	        setButton = new JButton("确 定");
	        setButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					try {
						setActionPerformed(evt);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
	        p2.add(setButton);
	        getContentPane().add("South", p2);
	 
	        // 调整对话框布局大小
	        pack();
	    }
	    
	    private void setActionPerformed(java.awt.event.ActionEvent evt) throws IOException {// GEN-FIRST:event_startActionPerformed
			String username = field.getText();
			long diffSec = diff / (1000);
			
			File datafile = new File("D:\\data.txt");
			FileWriter fileWriter = new FileWriter(datafile, true);
			//打开文件
			PrintWriter printWriter = new PrintWriter(fileWriter);
			
			
			printWriter.println(new User(username, diffSec));
			
			
			printWriter.close();
			
			this.dispose();
			
			work w = new work();
			w.setVisible(true);
			
			
			
		}// GEN-LAST:event_startActionPerformed
	    
	    
	    
	}

	private static int answer;
	private static java.util.Date start, end;
	private long diff;

	/**
	 * Creates new form work
	 */
	public work() {
		start = new java.util.Date();
		System.out.println(start);
		initComponents();
		answer = new Random().nextInt(99) + 1;
		System.out.println(answer);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		GES = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		AND = new javax.swing.JTextField();
		sure = new javax.swing.JToggleButton();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		jLabel1.setText("输入");

		GES.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				GESActionPerformed(evt);
			}
		});

		jLabel2.setText("输出");

		AND.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ANDActionPerformed(evt);
			}
		});

		sure.setText("确定");
		sure.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				sureActionPerformed(evt);
			}
		});

		jButton1.setText("退出");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addGap(147, 147, 147)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
								false)
								.addComponent(
										sure,
										javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
								.addGroup(layout
										.createSequentialGroup()
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel2))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
												.addComponent(GES, javax.swing.GroupLayout.DEFAULT_SIZE, 64,
														Short.MAX_VALUE)
												.addComponent(AND)))
								.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addContainerGap(156, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
				.createSequentialGroup().addGap(67, 67, 67)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(GES, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel1))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2)
						.addComponent(AND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(26, 26, 26).addComponent(sure).addGap(18, 18, 18).addComponent(jButton1)
				.addContainerGap(91, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void GESActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_GESActionPerformed

		// GES.getText();

	}// GEN-LAST:event_GESActionPerformed

	private void ANDActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_ANDActionPerformed

		// TODO add your handling code here:
	}// GEN-LAST:event_ANDActionPerformed

	private void sureActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_sureActionPerformed
		int g = Integer.parseInt(GES.getText());

		if (g != answer) {
			if (g < answer) {
				AND.setText("too small");
			} else if (g > answer) {
				AND.setText("too big");

			}
		} else {
			end = new java.util.Date();
			diff = end.getTime() - start.getTime();
			AND.setText("right");
			
			this.setVisible(false);
			
			new MyDialog().setVisible(true);
		}

		// TODO add your handling code here:
	}// GEN-LAST:event_sureActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		// TODO adwork w=new work();
		GNB o = new GNB();
		o.setVisible(true);
		this.setVisible(false);

	}// GEN-LAST:event_jButton1ActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
/*	public static void main(String args[]) {
		 Set the Nimbus look and feel 
		// <editor-fold defaultstate="collapsed" desc=" Look and feel setting
		// code (optional) ">
		
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
		 * html
		 
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		 Create and display the form 
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new work().setVisible(true);

			}
		});
	}*/

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JTextField AND;
	private javax.swing.JTextField GES;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JToggleButton sure;
	// End of variables declaration//GEN-END:variables
}
